package com.zyzd.baseprojectofvolly.base.activity;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zyzd.baseprojectofvolly.Constant;
import com.zyzd.baseprojectofvolly.interf.UiBaseInterface;

import butterknife.ButterKnife;


public abstract class BaseActivity extends AppCompatActivity implements UiBaseInterface {


    protected LocalBroadcastManager broadcastManager;
    protected BroadcastReceiver broadcastReceiver;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSelfTheme();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        initData();
        setListener();
        registerBroadcastReceiver();
    }

    protected void setSelfTheme() {
    }


    /**
     * 注册本地广播
     */
    protected void registerBroadcastReceiver() {
        broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_CLOSE_ALL_ACTIVITY);//关闭activity
        broadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                forceFinishActivity();
            }
        };
        broadcastManager.registerReceiver(broadcastReceiver, intentFilter);//注册广播
    }

    protected void forceFinishActivity() {
        BaseActivity.this.finish();
    }

    /**
     * 注册本地广播
     */
    private void unregisterBroadcastReceiver() {
        broadcastManager.unregisterReceiver(broadcastReceiver);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        unregisterBroadcastReceiver();
    }


    //----------------------------------工具方法------------------------------------

    /**
     * 找控件的方法
     */
    protected <T extends View> T findView(int viewId) {
        return (T) findViewById(viewId);
    }

    protected void setText(int viewId, String text) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            return;
        }
        textView.setText(text);
    }

    protected void setText(int viewId, String text, String emptyTip) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            textView.setText(emptyTip);
            return;
        }
        textView.setText(text);
    }

    protected void setTextEmptyGone(int viewId, String text) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            textView.setVisibility(View.GONE);
            return;
        }
        textView.setText(text);
    }

    protected void setGone(int id) {
        findView(id).setVisibility(View.GONE);
    }

    protected void setVisibility(int id) {
        findView(id).setVisibility(View.VISIBLE);
    }

    protected void setInVisibility(int id) {
        findView(id).setVisibility(View.INVISIBLE);
    }


    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    public String getSimpleName() {
        return getClass().getSimpleName();
    }
}
